package com.team3.project.controller;

import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.team3.project.model.Users;
import com.team3.project.validator.LoginValidator;

@Controller
public class LoginController {
	
	private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	private LoginValidator loginValidator;
	
	@RequestMapping(value = {"/", "/index", "/login"}, method = RequestMethod.GET)
	public String login(Locale locale) {
		logger.info("Initial login. The client locale is {}.", locale);
		return "login";
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String userLogin(@RequestParam(value="username", required=false) String username,
			@RequestParam(value="password", required=false) String password) {

		Users user = loginValidator.verifyUser(username, password);
		
		if(user.getRole() == "Admin"){
			return "redirect:/adminHome";
		} else if(user.getRole() == "Staff" || user.getRole() == "Manager"){
			return "redirect:/staffHome";
		} else {
			return "redirect:/loginerror";
		}
	}	
	
	@RequestMapping(value = "/loginerror", method = RequestMethod.GET)
	public String loginError(Locale locale) {
		logger.info("Logged in failed. The client locale is {}.", locale);
		return "loginError";
	}


}
